<template>
  <!--引用表格-->
  <BasicTable @register="registerTable" :dataSource="dataSource">
    <!--插槽:table标题-->
    <template #tableTitle>
      <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 新增</a-button>
    </template>
    <!--操作栏-->
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <!--全局变量弹窗-->
  <FunctionModal @register="registerModal" @success="handleSuccess" />
</template>

<script lang="ts" setup>
  //ts语法
  import { ref, onMounted } from 'vue';
  import { BasicTable, TableAction } from '/src/components/Table';
  import { useModal } from '/src/components/Modal';
  import FunctionModal from './components/functionModal.vue';
  import { columns, searchFormSchema } from './functions.data';
  import { list, deleteFunction } from './functions.api';
  const dataSource = ref([]);
  //字典model
  const [registerModal, { openModal }] = useModal();
  import { useListPage } from '/@/hooks/system/useListPage';

  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    designScope: 'rpa-function-template',
    tableProps: {
      title: '全局变量',
      api: list,
      columns: columns,
      showIndexColumn: true,
      formConfig: {
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 240,
      },
    },
  });

  // 注册table数据
  const [registerTable, { reload, updateTableDataRecord }] = tableContext;

  /**
   * 新增事件
   */
  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }
  /**
   * 编辑事件
   */
  async function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteFunction({ id: record.id }, reload);
  }
  /**
   * 成功回调
   */
  function handleSuccess({ isUpdate, values }) {
    if (isUpdate) {
      updateTableDataRecord(values.id, values);
    } else {
      reload();
    }
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '确定删除吗?',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }
</script>

<style scoped></style>
